﻿//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

#include "pch.h"
#include "MainPage.xaml.h"
#include "..\winrtsock\include\winrtsock.h"
#pragma comment(lib, "winrtsock.lib")
#pragma comment(lib, "livemedia.lib")
#include <ppltasks.h>
using namespace WinAppTest;
using namespace concurrency;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Networking;

//Exported from live555 dll
int TestLive555();

MainPage::MainPage()
{
	InitializeComponent();
}


void WinAppTest::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    //Yeah its a leak, but its just a test =X
    char * server_reply = new char[200000];
   
    task<void>([server_reply]
    {
        //This method does not return
        TestLive555();

        //Other winsock tests
        WSADATA wsa;
        SOCKET s;
        struct sockaddr_in server;
        char *message;
        int recv_size;

        //Init winsock
        if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
        {
            return;
        }
        //make a socket
        if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        {
            return;
        }

        server.sin_addr.s_addr = inet_addr("74.125.224.226");
        server.sin_family = AF_INET;
        server.sin_port = htons(80);

        //connect to server
        if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            return;
        }

        ////send request
        message = "GET / HTTP/1.1\r\n\r\n";
        if (send(s, message, strlen(message), 0) < 0)
        {
            return;
        }

        //Get reply
        if ((recv_size = recv(s, server_reply, 200000, 0)) == SOCKET_ERROR)
        {
            int x = WSAGetLastError();
            return;
        }

        int x = WSAGetLastError();
    });
}
